import { constantRoutes, asyncRoutes } from '../../router/index'

const permission = {
  state: {
    routers: constantRoutes,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers.concat(asyncRoutes[1])
      // state.routers = constantRoutes.concat(routers)
    }
  },
  actions: {
    GenerateRoutes ({ commit }, data) {
      let pages = data.map(item => {
        return item.name
      })
      return new Promise(resolve => {
        let accessedRouters = {...asyncRoutes[0], children: []}
        asyncRoutes[0].children.filter((route, i) => {
          if (pages.indexOf(route.name) > -1) {
            let index = pages.indexOf(route.name)
            if (data[index].children) route.meta.children = data[index].children
            accessedRouters.children.push(route)
          }
        })
        // console.log(accessedRouters)
        commit('SET_ROUTERS', [accessedRouters])
        resolve()
      })
    }
  }
}

export default permission
